package com.hx.myapp.datastorage;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.hx.myapp.R;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

public class FileActivity extends AppCompatActivity {
    private Button mBtnSave,mBtnShow;
    private EditText mEtName;
    private TextView mTvContent;
    private final String mFileName ="test.txt";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_file);
        mEtName = findViewById(R.id.et_name);
        mBtnSave =findViewById(R.id.btn_save);
        mBtnShow = findViewById(R.id.btn_show);
        mTvContent = findViewById(R.id.tv_content);


        mBtnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               save(mEtName.getText().toString().trim());
            }
        });

        mBtnShow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              mTvContent.setText(read());
            }
        });
    }
    private void save(String content){
        FileOutputStream fileOutputStream = null;
        try {
            // 内部存储
           // fileOutputStream = openFileOutput(mFileName,MODE_PRIVATE);
            // 外部存储
            String path = getApplicationContext().getExternalFilesDir(null).getAbsolutePath();
            File dir = new File(path,"skypan");
            if(!dir.exists()){
                dir.mkdir();
                Log.d("创建目录","成功");
            }
            File file = new File(dir,mFileName);
            if(!file.exists()){
                file.createNewFile();
            }
            fileOutputStream = new FileOutputStream(file);
            fileOutputStream.write(content.getBytes());
            Log.d("fileOutputStream","写入成功");

        }  catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(fileOutputStream != null){
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
    private String read(){
        FileInputStream fileInputStream = null;
        try {
         //  fileInputStream = openFileInput(mFileName);
           File file = new File(getApplicationContext().getExternalFilesDir(null).getAbsolutePath() +File.separator +"skypan",mFileName);
           fileInputStream = new FileInputStream(file);
           byte[] buff = new byte[1024];
           StringBuffer sb = new StringBuffer("");
           int len = 0;
           while ((len = fileInputStream.read(buff))> 0){
               sb.append(new String(buff,0,len));
           }
            Log.d("fileInputStream","读取成功"+ sb.toString());
           return sb.toString();

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(fileInputStream != null){
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }
}